package com.qianfeng.gl4study.youshare.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.qianfeng.gl4study.youshare.R;
import com.qianfeng.gl4study.youshare.model.Product;
import com.qianfeng.gl4study.youshare.utils.Constants;
import com.qianfeng.gl4study.youshare.utils.FileCache;
import com.qianfeng.gl4study.youshare.utils.ImageCache;
import com.qianfeng.gl4study.youshare.utils.ImageLoader;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/7
 * Email:pdsfgl@live.com
 */
public class StorePlatformAdapter extends BaseAdapter {
	private List<Product> productList;
	private LayoutInflater inflater;

	public StorePlatformAdapter(Context context,List<Product> productList) {
		this.productList = productList;
		if(context==null){
			throw new IllegalArgumentException("Context must not null");
		}
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {

		int ret = 0;
		if(productList!=null){
			ret = productList.size();
		}
		return ret;
	}

	@Override
	public Object getItem(int position) {
		Object ret = null;
		if(productList!=null){
			ret = productList.get(position);
		}
		return ret;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View ret = null;

		if(convertView!=null){
			ret = convertView;
		}else {
			ret = inflater.inflate(R.layout.item_store_platform, parent, false);
		}
		ViewHolder holder = (ViewHolder)ret.getTag();
		if(holder==null){
			holder = new ViewHolder();
			holder.imgIcon = (ImageView)ret.findViewById(R.id.item_platform_icon);
			holder.txtTitle = (TextView)ret.findViewById(R.id.item_platform_title);
			holder.txtPrice = (TextView)ret.findViewById(R.id.item_platform_price);
			ret.setTag(holder);
		}

		Product product = productList.get(position);

		holder.txtTitle.setText(product.getName());
		holder.txtPrice.setText(product.getShopPrice());

		//加载图片
		/*
		获取缩略图地址
		 */
		holder.imgIcon.setImageResource(R.drawable.ic_launcher);
		String thumb = product.getGoodsThumb();

		if (thumb != null && thumb.length() > 0) {
			if (!thumb.startsWith("http://") && !thumb.startsWith("https://")) {
				thumb = Constants.IMAGE_SERVICE + thumb;
			}

			ImageCache imageCache = ImageCache.getInstance();

			Bitmap bitmap = imageCache.getImage(thumb);

			if (bitmap != null) {
				holder.imgIcon.setImageBitmap(bitmap);
			} else {
				// TODO 查看文件缓存
				FileCache fileCache = FileCache.getInstance();

				byte[] content = fileCache.getContent(thumb);

				if (content != null && content.length > 0) {
					// 生成 Bitmap ，并且更新UI

					bitmap = BitmapFactory.decodeByteArray(content, 0, content.length);

					imageCache.putImage(thumb, bitmap);

					holder.imgIcon.setImageBitmap(bitmap);
				}else{
					// TODO 进行网络下载

					ImageLoader task = new ImageLoader(holder.imgIcon);
					task.execute(thumb);

				}

			}

		}
		return ret;
	}

	private  static  class ViewHolder{
		public TextView txtPrice;
		public TextView txtTitle;
		public ImageView imgIcon;
	}
}
